<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>查找对话框布局和测试页面</title>
    <link rel="stylesheet" href="../../css/normalize.css">
    <link rel="stylesheet" href="../../css/dlg-common.css">
    <link rel="stylesheet" href="dlg-search.css">
    <link rel="icon" href="https://i.wangding.in/favicon.ico" sizes="48x48" type="image/png">
    <style>
        .notepad-dlg-search {
            z-index: 10;
        }

        .notepad-dlg-search .dialogbox {
            height: 164px;
            width: 414px;
        }

        .notepad-dlg-search .main {
            height: 132px;
            padding: 6px;
        }

        .notepad-dlg-search .main .txt-content {
            box-sizing: border-box;
            height: 26px;
            margin: 10px;
            width: 224px;
        }

        .notepad-dlg-search .main .btn-search {
            position: absolute;
            right: 8px;
            top: 12px;
        }

        .notepad-dlg-search .main .btn-cancel {
            bottom: 56px;
            position: absolute;
            right: 8px;
        }

        .notepad-dlg-search .main .search-direction {
            border: 1px solid #dcdcdc;
            box-sizing: border-box;
            height: 52px;
            left: 172px;
            padding: 10px 5px;
            position: absolute;
            top: 64px;
            width: 136px;
        }

        .notepad-dlg-search .main input[value="down"] {
            margin-left: 5px;
        }

        .notepad-dlg-search .main input[value="capital-sense"] {
            margin-top: 30px;
        }
    </style>
</head>

<body>
    <div class="test">
        <input type="button" value="弹出对话框" autofocus>
    </div>
    <!--
<div class="notepad-dlg-search">
  <div class="dialogbox notepad-dlgbox">
    <div class="notepad-dlg-titlebar">
      <p class="title">查找</p>
      <span class="close-btn">✖</span>
    </div>
    <div class="main notepad-dlg-main">
      <label for="">查找内容(N):</label>
      <input class="txt-content" type="text" autofocus><br>
      <input type="checkbox" value="capital-sense">区分大小写(C)
      <fieldset class="search-direction">
        <legend>方向</legend>
        <input type="radio" value="up">向上(U)
        <input type="radio" value="down" checked>向下(D)
      </fieldset>
      <input class="btn-search btn" type="button" value="查找下一个(F)">
      <input class="btn-cancel btn" type="button" value="取消">
    </div>
  </div>
</div>
-->
    <script src="//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src="//cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.min.js"></script>
    <script src="dlg-search.js"></script>
    <script>
        $(function () {
            var $btnTest = $('div.test>input[type="button"]');

            $btnTest.click(function () {
                $dlgSearch.show(function (content) {
                    console.log("search btn click!");
                    console.log(content);
                });
            });
        });
    </script>
    <script>
        /* exported $dlgSearch */
        var $dlgSearch = (function () {
            var $dlg = $(''
                + '<div class="notepad-dlg-search">'
                + '<div class="dialogbox notepad-dlgbox">'
                + '<div class="notepad-dlg-titlebar">'
                + '<p class="title">查找</p>'
                + '<span class="close-btn">✖</span>'
                + '</div>'
                + '<div class="main notepad-dlg-main">'
                + '<label>查找内容(N): <input class="txt-content" type="text" autofocus></label><br>'
                + '<label><input type="checkbox" value="capital-sense">区分大小写(C)</label>'
                + '<fieldset class="search-direction">'
                + '<legend>方向</legend>'
                + '<label><input type="radio" name="direction" value="up">向上(U)</label>'
                + '<label><input type="radio" name="direction" value="down" checked>向下(D)</label>'
                + '</fieldset>'
                + '<input class="btn-search btn" type="button" value="查找下一个(F)" disabled>'
                + '<input class="btn-cancel btn" type="button" value="取消">'
                + '</div>'
                + '</div>'
                + '</div>');

            var $btnClose = $dlg.find('.close-btn'),
                $btnCancel = $dlg.find('.btn-cancel'),
                $btnSearch = $dlg.find('.btn-search'),
                $txtContent = $dlg.find('.txt-content'),
                $titleBar = $dlg.find('.notepad-dlg-titlebar');

            function destoryDlg() { $dlg.remove(); }

            function verify() {
                if ($txtContent.val() !== '') {
                    $btnSearch.removeAttr('disabled');
                } else {
                    $btnSearch.attr('disabled', 'disabled');
                }
            }

            function init() {
                $dlg.find('.dialogbox').draggable({ handle: $titleBar });
                $dlg.find('input[value="up"]').removeAttr('checked');
                $dlg.find('input[value="down"]')[0].checked = true;
                $dlg.find('input[type="checkbox"]').removeAttr('checked');
                $btnSearch.attr('disabled', 'disabled');
                $txtContent.val('');
                $txtContent.focus();
            }

            function show(searchHandler) {
                $('body').append($dlg);
                init();

                $btnClose.click(destoryDlg);
                $btnCancel.click(destoryDlg);
                $txtContent.keyup(verify);
                $btnSearch.click(function () {
                    searchHandler({
                        content: $txtContent.val(),
                        capitalSense: $dlg.find('input[type="checkbox"]:checked').val() === 'capital-sense',
                        direction: $dlg.find('input[name="direction"]:checked').val()
                    });
                });
            }

            return { show: show };
        })();
    </script>
</body>

</html>